import { defineStore } from "pinia";

export const useNodeDataStore = defineStore({
    id: "node_data",
    state() {
        return {
            nodes: [
                {
                    id: 57,
                    nodeIp: "192.168.1.1",
                    cpuUtilization: 50,
                    memoryUtilization: 75,
                    congestionLevel: 30,
                    target: "[59,61]",
                    diversionPercentage: "[0.38918689,0.15528279]",
                    diversion: false
                },
                {
                    id: 61,
                    cpuUtilization: 0.2,
                    congestionLevel: 0.2,
                    memoryUtilization: 0.3,
                    nodeIp: '198.0.0.1',
                    target: "[59]",
                    diversionPercentage: "[10]",
                    diversion: false
                },
                {
                    id: 59,
                    cpuUtilization: 0.3,
                    congestionLevel: 0.3,
                    memoryUtilization: 0.4,
                    nodeIp: '198.0.0.1',
                    target: "0",
                    diversionPercentage: "0",
                    diversion: false
                },
                {
                    id: 1,
                    cpuUtilization: 0.3,
                    congestionLevel: 0.3,
                    memoryUtilization: 0.4,
                    nodeIp: '198.0.0.1',
                    target: "0",
                    diversionPercentage: "0",
                    diversion: false
                },
                {
                    id: 2,
                    cpuUtilization: 0.3,
                    congestionLevel: 70,
                    memoryUtilization: 0.4,
                    nodeIp: '198.0.0.1',
                    target: "0",
                    diversionPercentage: "0",
                    diversion: false
                },
            ],
        };
    },
    actions: {
        getRandomNode() {
            // 随机生成 target 数组，长度为 1 到 3 个随机目标
            let targetCount = Math.floor(Math.random() * 3) + 1;
            let target = Array.from({ length: targetCount }, () => Math.floor(Math.random() * 100));

            // 根据 target 的数量生成相应长度的 diversionPercentage 数组
            let diversionPercentage = target.map(() => parseFloat(Math.random().toFixed(8)));

            let node = {
                id: Math.floor(Math.random() * 100),
                nodeIp: "198.0.0." + Math.floor(Math.random() * 100),
                cpuUtilization: parseFloat(Math.random().toFixed(1)),
                congestionLevel: Math.floor(Math.random() * 100),
                memoryUtilization: parseFloat(Math.random().toFixed(1)),
                target: target,
                diversionPercentage: diversionPercentage,
                diversion: Math.random() > 0.5, // 随机 true 或 false
            };

            return node;
        }
    }
});